//
//  ChooseResultViewController.m
//  CarUser
//
//  Created by 王森林 on 2019/1/10.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "ChooseResultViewController.h"
#import "ChooseResultCell.h"
#import "ShopConditionViewController.h"
#import "ChooseView.h"
@interface ChooseResultViewController ()
@property (nonatomic, strong) NSMutableArray *dataAry;
@property (nonatomic, strong) NSString *orderByStr;
@property (nonatomic, strong) ShopConditionViewController *chooseController;
@property  (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) NSString *isSales;
@property (nonatomic, strong) Custom_dznEmptyDelegate *custom_emptyView;
@property (nonatomic, assign) NSInteger showBtn;
@property (nonatomic, strong) ChooseView *chooseView;
@property  (nonatomic, strong) UIButton *oprBtn; //正在操作的按钮
@property  (nonatomic, strong) UIButton *preBtn; //前面的按钮
@property (nonatomic, assign) NSInteger salesIndex;
@property (nonatomic, assign) NSInteger priceNIndex;
@property (nonatomic, assign) NSInteger dateIndex;
@end
#define BTNTAG 10000
@implementation ChooseResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选车结果";
    self.orderByStr = @"11";
    self.salesIndex = 0;
      self.priceNIndex = 0;
      self.dateIndex = 0;
//    self.sfrx00 = @"";
//    self.jb0000 = @"";
//    self.csjg00 = @"";
//    self.zdjgmax = @"";
//    self.zdjgmin = @"";
//     self.keywords = @"";
    if (self.pushType == 1) {
        self.jb0000Index = -1;
        self.csjg00Index = -1;
          self.priceIndex = -1;
    }
 
    [self createUI];
    [self createHeadView];
    [self fetchDataFromNet];
    
    self.showBtn  = 1;
    self.mainTableView.mj_header = [Custom_Mjfresh mj_customMjfreshHeaderSetting:^{
        [self fetchDataFromNet];
    }];
//    Custom_Mjfresh
    
    // Do any additional setup after loading the view.
}


- (void)fetchDataFromNet
{
    //获取广告顶部
//    NSDictionary *params = @{
//                             @"FKEY":[CommonMethod createMD5Key:@"NULL"]
//
//                             };
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:[CommonMethod createMD5Key:@"NULL"] forKey:@"FKEY"];
    if (![CommonMethod isBlankString:self.sfrx00]) {
        [params setObject:self.sfrx00 forKey:@"sfrx00"];
    }
    if (![CommonMethod isBlankString:self.isSales]) {
        if (self.isSales.integerValue != 0 ) {
             [params setObject:self.isSales forKey:@"zszt00"];
        }
    }
    
    if (self.type.integerValue == 2) {
        if (![CommonMethod isBlankString:self.SERIES_ID]) {
            [params setObject:self.SERIES_ID forKey:@"sscx00"];
            
        }
    }
    
    if (self.type.integerValue == 3) {
        if (![CommonMethod isBlankString:self.SERIES_ID]) {
            [params setObject:self.SERIES_ID forKey:@"sspp00"];
            
        }
    }
    
    if (![CommonMethod isBlankString:self.jb0000]) {
        [params setObject:self.jb0000 forKey:@"jb0000"];
    }
    
    if (![CommonMethod isBlankString:self.csjg00]) {
        [params setObject:self.csjg00 forKey:@"csjg00"];
    }
    if (![CommonMethod isBlankString:self.zdjgmin]) {
        [params setObject:self.zdjgmin forKey:@"zdjgmin"];
    }
    
    if (![CommonMethod isBlankString:self.zdjgmax]) {
        [params setObject:self.zdjgmax forKey:@"zdjgmax"];
    }
    
    if (![CommonMethod isBlankString:self.keywords]) {
        [params setObject:self.keywords forKey:@"keywords"];
    }
    
    if (![CommonMethod isBlankString:self.orderByStr]) {
        [params setObject:self.orderByStr forKey:@"orderByStr"];
    }
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_getSaleCarList params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        [self.mainTableView.mj_header endRefreshing];
        [self.dataAry removeAllObjects];
        NSDictionary *resultDict  = response;
        NSArray *ary = [resultDict objectForKey:@"list"];
//        ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0
//        [ary isKindOfClass:[NSArray class]] && ary.count > 0
        if (ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0)
        {
            [self hideNoDataView];
            self.dataAry  = [NSMutableArray arrayWithArray:[resultDict objectForKey:@"list"]];
           
        } else {

            [self showNodataView:self.mainTableView.centerY-150 withView:self.mainTableView];
  
        }
         [self.mainTableView reloadData];
       

        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
              [self.mainTableView.mj_header endRefreshing];
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
}

- (void)createHeadView
{
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 45.0f)];
    headView.backgroundColor = Common_BackgroundColor;
    [self.view addSubview:headView];
    
    self.mainTableView.frame =CGRectMake(0, 45, BOUNDWIDTH, BOUNDHEIGHT-45-NAV_HEIGHT);
    
    //精选
    UIButton *hotBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    hotBtn.frame = CGRectMake(15, 0, 50, 45);
    hotBtn.titleLabel.font = FONT_SIZE(14);
    hotBtn.tag = BTNTAG ;
    [hotBtn setTitle:@"精选" forState:UIControlStateNormal];
    [hotBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [hotBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [hotBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:hotBtn];
    if (self.sfrx00.intValue == 1) {
        hotBtn.selected = YES;
    } else {
        hotBtn.selected = NO;
        
    }
    
    
    //筛选
    UIButton *selectBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
    selectBtn.frame = CGRectMake(BOUNDWIDTH -  75 , 0, 60, 45);
    selectBtn.titleLabel.font = FONT_SIZE(14);
    [selectBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [selectBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [selectBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [selectBtn setImage:IMG(@"screen") forState:UIControlStateNormal];
    [selectBtn setImage:IMG(@"screen_select") forState:UIControlStateSelected];
    [selectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - selectBtn.imageView.image.size.width, 0, selectBtn.imageView.image.size.width)];
    [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, selectBtn.titleLabel.bounds.size.width, 0, -selectBtn.titleLabel.bounds.size.width)];
    selectBtn.tag= BTNTAG + 4;
    [selectBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:selectBtn];
    
    //销量从高到低
    UIButton *salesBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
    salesBtn.frame = CGRectMake(65 , 0,( BOUNDWIDTH - 140)/3, 45);
    salesBtn.titleLabel.font = FONT_SIZE(14);
    [salesBtn setTitle:@"销量" forState:UIControlStateNormal];
//    [salesBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [salesBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [salesBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [salesBtn setImage:IMG(@"common_select") forState:UIControlStateNormal];
    [salesBtn setImage:IMG(@"up_select") forState:UIControlStateSelected];
    salesBtn.selected = YES;
    [salesBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - salesBtn.imageView.image.size.width, 0, salesBtn.imageView.image.size.width)];
    [salesBtn setImageEdgeInsets:UIEdgeInsetsMake(0, salesBtn.titleLabel.bounds.size.width, 0, -salesBtn.titleLabel.bounds.size.width-8)];
    salesBtn.tag= BTNTAG + 1;
    [salesBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:salesBtn];
    self.preBtn = salesBtn;
    
    //价格
    UIButton *priceBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
    priceBtn.frame = CGRectMake(65+( BOUNDWIDTH - 140)/3 , 0,( BOUNDWIDTH - 140)/3, 45);
    priceBtn.titleLabel.font = FONT_SIZE(14);
    [priceBtn setTitle:@"价格" forState:UIControlStateNormal];
    //    [salesBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [priceBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [priceBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [priceBtn setImage:IMG(@"common_select") forState:UIControlStateNormal];
    [priceBtn setImage:IMG(@"up_select") forState:UIControlStateSelected];
    [priceBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - salesBtn.imageView.image.size.width, 0, salesBtn.imageView.image.size.width)];
    [priceBtn setImageEdgeInsets:UIEdgeInsetsMake(0, salesBtn.titleLabel.bounds.size.width, 0, -salesBtn.titleLabel.bounds.size.width-8)];
    priceBtn.tag= BTNTAG + 2;
    [priceBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:priceBtn];
    
    
    //上架时间顺序
    UIButton *upBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
    upBtn.frame = CGRectMake(( BOUNDWIDTH - 140)/3*2+65 , 0, ( BOUNDWIDTH - 140)/3, 45);
    upBtn.titleLabel.font = FONT_SIZE(14);
    [upBtn setTitle:@"上架时间" forState:UIControlStateNormal];
    [upBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
    [upBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [upBtn setImage:IMG(@"common_select") forState:UIControlStateNormal];
    [upBtn setImage:IMG(@"up_select") forState:UIControlStateSelected];
    [upBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - upBtn.imageView.image.size.width, 0, upBtn.imageView.image.size.width)];
    [upBtn setImageEdgeInsets:UIEdgeInsetsMake(0, upBtn.titleLabel.bounds.size.width, 0, -upBtn.titleLabel.bounds.size.width-8)];
    upBtn.tag= BTNTAG + 3;
    [upBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:upBtn];
    
    
//    return headView;
}

- (void)createUI
{
    
    self.mainTableView.estimatedRowHeight = 270;
    [self.mainTableView registerClass:[ChooseResultCell class] forCellReuseIdentifier:@"ChooseResultCell"];
    

    
}

- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
 
    NSInteger tag = btn.tag - BTNTAG;
    switch (tag) {
        case 0:
        {
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            [self.chooseView removeFromSuperview];
            self.chooseView = nil;
            
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 170), BOUNDWIDTH,BOUNDHEIGHT - 170);
            [self.chooseController.view removeFromSuperview];
            [self.chooseController removeFromParentViewController];
            self.jb0000 = @"";
            self.csjg00 = @"";
            self.zdjgmax = @"";
            self.zdjgmin = @"";
            UIButton *btn4 = [self.view viewWithTag:BTNTAG  + 4];
            btn4.selected  = NO;
            if (!btn.selected) {
                btn.selected = YES;
                self.sfrx00 = @"1";
            } else {
                btn.selected = NO;
                self.sfrx00 = @"";
            }
            [self.dataAry removeAllObjects];
            [self fetchDataFromNet];
        }
            break;
        case 1:
        {
       //原来代码
//            if (!btn.selected) {
//                btn.selected = YES;
//                self.orderByStr = @"11";
//            } else {
//                btn.selected = NO;
//               self.orderByStr = @"12";
//            }
//
//            UIButton *btn2 = [self.view viewWithTag:BTNTAG + 2];
//            btn2.selected = NO;
//            [btn2 setTitleColor:Common_TextColor forState:UIControlStateNormal];
//            [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//            [btn2 setImage:IMG(@"common_select") forState:UIControlStateNormal];
//            [btn2 setImage:IMG(@"down_select") forState:UIControlStateSelected];
//            if (self.showBtn == 2) {
//                [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//                [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//                [btn setImage:IMG(@"down_select") forState:UIControlStateNormal];
//                [btn setImage:IMG(@"up_select") forState:UIControlStateSelected];
//                self.showBtn = 1;
//            }
//
//
//
//
            
            if (self.salesIndex == 0) {
                self.orderByStr = @"11";
            } else {
                self.orderByStr = @"12";
            }
            [self.dataAry removeAllObjects];
            [self fetchDataFromNet];
            
             btn.selected = YES;
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            [self.chooseView removeFromSuperview];
            self.chooseView = nil;
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 170), BOUNDWIDTH,BOUNDHEIGHT - 170);
            [self.chooseController.view removeFromSuperview];
            [self.chooseController removeFromParentViewController];
            self.jb0000 = @"";
            self.csjg00 = @"";
            self.zdjgmax = @"";
            self.zdjgmin = @"";
            UIButton *btn4 = [self.view viewWithTag:BTNTAG  + 4];
            btn4.selected  = NO;
            if (btn.selected) {
//                btn.selected = ;
                
                if (self.chooseView) {
                    [self.backgroundView  removeFromSuperview];
                    self.backgroundView = nil;
                    [self.chooseView removeFromSuperview];
                    self.chooseView = nil;
                } else {
                    
//                    NSInteger index = 0;
//                    if (self.orderByStr.integerValue  == 11) {
//                        index = 0;
//                    }
//                    if (self.orderByStr.integerValue  == 12) {
//                        index = 1;
//                    }
//
                    self.chooseView = [[ChooseView alloc] initWithFrame:CGRectMake(0,45, BOUNDWIDTH, 0) withData:@[@"销量从高到低",@"销量从低到高"] withIndex:self.salesIndex  withBlock:^(id  _Nonnull sales) {
                        NSString  *index = sales;
                        if (index.integerValue == 1) {
                            self.orderByStr = @"11";
                            self.salesIndex = 0;
                        }
                        
                        if (index.integerValue == 2) {
                            self.orderByStr = @"12";
                            self.salesIndex = 1;
                        }
                        
                        [self.backgroundView  removeFromSuperview];
                        self.backgroundView = nil;
                        [self.chooseView removeFromSuperview];
                        self.chooseView = nil;
                        [self.dataAry removeAllObjects];
                        [self fetchDataFromNet];

                        
                    }];
//                    self.chooseView.hidden = YES;
                    self.chooseView.backgroundColor = [UIColor whiteColor];
                  
                   
                    [UIView animateWithDuration:.6 animations:^{
                        
                        self.chooseView.frame = CGRectMake(0, 45, BOUNDWIDTH, 90);
                       [self.view addSubview:self.chooseView];
//                        self.chooseView.hidden = NO;
                    } completion:^(BOOL finished) {
                        self.backgroundView.frame = CGRectMake(0, 45+90, BOUNDWIDTH, BOUNDHEIGHT - NAV_HEIGHT-45-90);
                        [self.view addSubview:self.backgroundView];
                        UITapGestureRecognizer *tap  =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackGesture:)];
                        [self.backgroundView addGestureRecognizer:tap];
                        
                       
                        [self.chooseView createUI];
                    }];
                    
                }
              

                

                
                
            } else {
//                btn.selected = NO;
                [self.backgroundView  removeFromSuperview];
                self.backgroundView = nil;
                [self.chooseView removeFromSuperview];
                self.chooseView = nil;
                
            }
            
            
            self.oprBtn = btn;
            
            if (self.preBtn) {
                if (self.oprBtn !=  self.preBtn) {
                    self.preBtn.selected  = NO;
                    self.preBtn = self.oprBtn;
                }
            
            }  else  {
                
                  self.preBtn = self.oprBtn;
            }
            
            
        }
            break;
        case 2:
        {
//            if (!btn.selected) {
//                btn.selected = YES;
//                self.orderByStr = @"21";
//            } else {
//                btn.selected = NO;
//                self.orderByStr = @"22";
//            }
//            self.showBtn = 2;
//            UIButton *btn1 = [self.view viewWithTag:BTNTAG + 1];
//            btn1.selected = NO;
//            [btn1 setTitleColor:Common_TextColor forState:UIControlStateNormal];
//            [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//            [btn1 setImage:IMG(@"common_select") forState:UIControlStateNormal];
//            [btn1 setImage:IMG(@"down_select") forState:UIControlStateSelected];
//            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//            [btn setImage:IMG(@"up_select") forState:UIControlStateNormal];
            if (self.priceNIndex == 0) {
                 self.orderByStr = @"31";
            } else {
                self.orderByStr = @"32";
            }
            
            [self.dataAry removeAllObjects];
            [self fetchDataFromNet];
            
            
             btn.selected = YES;
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            [self.chooseView removeFromSuperview];
            self.chooseView = nil;
            
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 170), BOUNDWIDTH,BOUNDHEIGHT - 170);
            [self.chooseController.view removeFromSuperview];
            [self.chooseController removeFromParentViewController];
            self.jb0000 = @"";
            self.csjg00 = @"";
            self.zdjgmax = @"";
            self.zdjgmin = @"";
            UIButton *btn4 = [self.view viewWithTag:BTNTAG  + 4];
            btn4.selected  = NO;
            if (btn.selected) {
                //                btn.selected = ;
                
                if (self.chooseView) {
                    [self.backgroundView  removeFromSuperview];
                    self.backgroundView = nil;
                    [self.chooseView removeFromSuperview];
                    self.chooseView = nil;
                } else {
                    
//                    NSInteger index = 0;
//                    if (self.orderByStr.integerValue  == 31) {
//                        index = 0;
//                    }
//                    if (self.orderByStr.integerValue  == 32) {
//                        index = 1;
//                    }
                    
                    self.chooseView = [[ChooseView alloc] initWithFrame:CGRectMake(0,45, BOUNDWIDTH, 0) withData:@[@"价格从高到低",@"价格从低到高"] withIndex:self.priceNIndex  withBlock:^(id  _Nonnull sales) {
                        NSString  *index = sales;
                        if (index.integerValue == 1) {
                            self.orderByStr = @"31";
                            self.priceNIndex = 0;
                        }
                        
                        if (index.integerValue == 2) {
                            self.orderByStr = @"32";
                            self.priceNIndex = 1;
                        }
                        
                        [self.backgroundView  removeFromSuperview];
                        self.backgroundView = nil;
                        [self.chooseView removeFromSuperview];
                        self.chooseView = nil;
                        [self.dataAry removeAllObjects];
                        [self fetchDataFromNet];

                        
                    }];
                    self.chooseView.hidden = YES;
                    self.chooseView.backgroundColor = [UIColor whiteColor];
//                    [self.view addSubview:self.chooseView];
                    [UIView animateWithDuration:.6 animations:^{
                        
                        self.chooseView.frame = CGRectMake(0, 45, BOUNDWIDTH, 90);
                        self.chooseView.hidden = NO;
                        [self.view addSubview:self.chooseView];
                    } completion:^(BOOL finished) {
                        self.backgroundView.frame = CGRectMake(0, 45+90, BOUNDWIDTH, BOUNDHEIGHT - NAV_HEIGHT-45-90);
                        [self.view addSubview:self.backgroundView];
                        UITapGestureRecognizer *tap  =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackGesture:)];
                        [self.backgroundView addGestureRecognizer:tap];
                        [self.chooseView createUI];
                    }];
                    
                }
                
                
                
                
                
                
            } else {
                //                btn.selected = NO;
                [self.backgroundView  removeFromSuperview];
                self.backgroundView = nil;
                [self.chooseView removeFromSuperview];
                self.chooseView = nil;
                
            }
            
            
            self.oprBtn = btn;
           
            if (self.preBtn) {
                if (self.oprBtn !=  self.preBtn) {
                    self.preBtn.selected  = NO;
                    self.preBtn = self.oprBtn;
                }
                
            } else {
                self.preBtn = self.oprBtn;
                
            }
        }
            break;
            
        case 3:
        {
//            if (!btn.selected) {
//                btn.selected = YES;
//                self.orderByStr = @"21";
//            } else {
//                btn.selected = NO;
//                self.orderByStr = @"22";
//            }
//            self.showBtn = 2;
//            UIButton *btn1 = [self.view viewWithTag:BTNTAG + 1];
//            btn1.selected = NO;
//            [btn1 setTitleColor:Common_TextColor forState:UIControlStateNormal];
//            [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//            [btn1 setImage:IMG(@"common_select") forState:UIControlStateNormal];
//            [btn1 setImage:IMG(@"down_select") forState:UIControlStateSelected];
//            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//            [btn setImage:IMG(@"up_select") forState:UIControlStateNormal];
            
            if (self.dateIndex == 0) {
                self.orderByStr = @"21";
            } else {
                self.orderByStr = @"22";
            }
            [self.dataAry removeAllObjects];
            [self fetchDataFromNet];
            
            
            
             btn.selected = YES;
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            [self.chooseView removeFromSuperview];
            self.chooseView = nil;
            
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 170), BOUNDWIDTH,BOUNDHEIGHT - 170);
            [self.chooseController.view removeFromSuperview];
            [self.chooseController removeFromParentViewController];
            self.jb0000 = @"";
            self.csjg00 = @"";
            self.zdjgmax = @"";
            self.zdjgmin = @"";
            UIButton *btn4 = [self.view viewWithTag:BTNTAG  + 4];
            btn4.selected  = NO;
            if (btn.selected) {
                //                btn.selected = ;
                
                if (self.chooseView) {
                    [self.backgroundView  removeFromSuperview];
                    self.backgroundView = nil;
                    [self.chooseView removeFromSuperview];
                    self.chooseView = nil;
                } else {
                    
//                    NSInteger index = 0;
//                    if (self.orderByStr.integerValue  == 21) {
//                        index = 0;
//                    }
//                    if (self.orderByStr.integerValue  == 22) {
//                        index = 1;
//                    }
                    
                    self.chooseView = [[ChooseView alloc] initWithFrame:CGRectMake(0,45, BOUNDWIDTH, 0) withData:@[@"上架时间从晚到早",@"上架时间从早到晚"] withIndex:self.dateIndex  withBlock:^(id  _Nonnull sales) {
                        NSString  *index = sales;
                        if (index.integerValue == 1) {
                             self.orderByStr = @"21";
                            self.dateIndex = 0;
                        }
                        
                        if (index.integerValue == 2) {
                               self.orderByStr = @"22";
                            self.dateIndex = 1;
                        }
                        
                        [self.backgroundView  removeFromSuperview];
                        self.backgroundView = nil;
                        [self.chooseView removeFromSuperview];
                        self.chooseView = nil;
                        
                        [self.dataAry removeAllObjects];
                        [self fetchDataFromNet];

                        
                    }];
                    self.chooseView.hidden = YES;
                    self.chooseView.backgroundColor = [UIColor whiteColor];
//                    [self.view addSubview:self.chooseView];
                    [UIView animateWithDuration:.6 animations:^{
                        
                        self.chooseView.frame = CGRectMake(0, 45, BOUNDWIDTH, 90);
                        self.chooseView.hidden = NO;
                         [self.view addSubview:self.chooseView];
                    } completion:^(BOOL finished) {
                        self.backgroundView.frame = CGRectMake(0, 45+90, BOUNDWIDTH, BOUNDHEIGHT - NAV_HEIGHT-45-90);
                        [self.view addSubview:self.backgroundView];
                        UITapGestureRecognizer *tap  =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackGesture:)];
                        [self.backgroundView addGestureRecognizer:tap];
                        [self.chooseView createUI];
                    }];
                    
                }
                
                
                
                
                
                
            } else {
                //                btn.selected = NO;
                [self.backgroundView  removeFromSuperview];
                self.backgroundView = nil;
                [self.chooseView removeFromSuperview];
                self.chooseView = nil;
                
            }
            
            
            self.oprBtn = btn;
//             btn.selected = YES;
            if (self.preBtn) {
                if (self.oprBtn !=  self.preBtn) {
                    self.preBtn.selected  = NO;
                    self.preBtn = self.oprBtn;
                }
                
            } else {
                
                self.preBtn = self.oprBtn;
            }
            
        }
            break;
        case 4:
        {
            
            [self.backgroundView  removeFromSuperview];
            self.backgroundView = nil;
            [self.chooseView removeFromSuperview];
            self.chooseView = nil;
            if (!btn.selected) {
                btn.selected = YES;
                if (self.chooseController == nil) {
                     self.chooseController = [[ShopConditionViewController alloc] init];
                }
                self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 220), BOUNDWIDTH,0);
                self.chooseController.view.hidden = YES;
                [self addChildViewController:self.chooseController];
                
                self.chooseController.resultBlock = ^(NSString * _Nonnull jb0000, NSString * _Nonnull csjg00, NSString * _Nonnull zdjgmin, NSString * _Nonnull zdjgmax) {
                    [self.backgroundView  removeFromSuperview];
                    self.backgroundView = nil;
                    self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 170), BOUNDWIDTH,BOUNDHEIGHT - 170);
                    [self.chooseController.view removeFromSuperview];
//                    [self.chooseController removeFromParentViewController];
                    self.jb0000 = jb0000;
                    self.csjg00 = csjg00;
                    self.zdjgmax = zdjgmax;
                    self.zdjgmin = zdjgmin;
                    self.jb0000Index = self.chooseController.jb0000Index;
                    self.csjg00Index = self.chooseController.csjg00Index;
                    self.priceIndex = self.chooseController.priceIndex;
                    [self fetchDataFromNet];
                    btn.selected = NO;
                    
                };
                
                [self.view addSubview:self.chooseController.view];
                 self.chooseController.view.hidden = NO;
                
                
                
                
                [UIView animateWithDuration:.6 animations:^{
                    
                    self.chooseController.view.frame = CGRectMake(0, 45, BOUNDWIDTH, BOUNDHEIGHT - 220);
                    self.chooseController.mainTableView.frame = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT - 275);
                    [self.chooseController initAll];
                    self.chooseController.jb0000Index = self.jb0000Index;
                    self.chooseController.csjg00Index = self.csjg00Index;
                    self.chooseController.priceIndex = self.priceIndex;
                } completion:^(BOOL finished) {
                    self.backgroundView.frame = CGRectMake(0, BOUNDHEIGHT - 175, BOUNDWIDTH, 175);
                    [self.view addSubview:self.backgroundView];
                }];
                

               
            } else {
                btn.selected = NO;
                
                [self.backgroundView  removeFromSuperview];
                self.backgroundView = nil;
                self.chooseController.view.frame = CGRectMake(0, 45-(BOUNDHEIGHT - 170), BOUNDWIDTH,BOUNDHEIGHT - 170);
                [self.chooseController.view removeFromSuperview];
                [self.chooseController removeFromParentViewController];
                self.jb0000 = @"";
                self.csjg00 = @"";
                self.zdjgmax = @"";
                self.zdjgmin = @"";
               
                
              
            }
         
            
            
        }
            break;
            
        default:
            break;
    }
    
    
}


- (void)handleBackGesture:(UIGestureRecognizer  *)gesture
{
    
//    self.oprBtn.selected = NO;
    [self.backgroundView  removeFromSuperview];
    self.backgroundView = nil;
    [self.chooseView removeFromSuperview];
    self.chooseView = nil;
    
}

-  (UIView *)backgroundView
{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, BOUNDHEIGHT - 205, BOUNDWIDTH, 100)];
        // 这个语句：遮罩层设置透明度之后，不会影响遮罩层上面的视图的透明度
        _backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
      
//       _backgroundView.hidden = YES;
        
    }
    return _backgroundView;
}

#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataAry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseResultCell *cell = (ChooseResultCell *)[tableView dequeueReusableCellWithIdentifier:@"ChooseResultCell" forIndexPath:indexPath];
    
    [cell setData:self.dataAry[indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;


    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
